package chapter05.Example09;

public class Example09 {
    private static final int TIMES = 100000;

    public static void main(String[] args) {
        Example09.testString();
        Example09.tesrStringBuffer();
        Example09.tesrStringBuilder();
    }
    //String时间效率测试
    public static void testString() {
        long starTime = System.currentTimeMillis();
        String str = "";
        for (int i = 0; i < TIMES; i++) {
            str += "test";
        }
        long endTime = System.currentTimeMillis();
        System.out.println("String test usedtime:" + (endTime - starTime));
    }
    //StringBuffer时间效率测试(线程安全)
    public static void tesrStringBuffer() {
        long starTime = System.currentTimeMillis();
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < TIMES; i++) {
            str.append("test");
        }
        long endTime = System.currentTimeMillis();
        System.out.println("StringBuffer test usedtime:" + (endTime - starTime));
    }
    //StringBuffer时间效率测试(非线程安全)
    public static void tesrStringBuilder() {
        long starTime = System.currentTimeMillis();
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < TIMES; i++) {
            str.append("test");
        }
        long endTime = System.currentTimeMillis();
        System.out.println("StringBuffer test usedtime:" + (endTime - starTime));
    }
}
